<!DOCTYPE html>
<html lang="en" xml:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>课程订阅页面</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <link rel="stylesheet" href="/css/custom.form.css">
</head>
<body>
<!--数据与分页-->
<div class="course_table_div">
    <!--多条件查询-->
    <div id="searchParam">
        <div class="layui-form-item">
            <div class="layui-input-inline">
                <input type="text" id="name" class="layui-input" autocomplete="off" placeholder="请输入课程名">
            </div>
            <div class="layui-input-inline ">
                <button class="layui-btn" id="search">查询</button>
            </div>
        </div>
    </div>
    <h2> 未订阅列表 </h2>
    <!--未订阅课程列表-->
    <table class="layui-hide" id="notSubscribed_course_table" lay-filter="notSubscribed_course_table"></table>
    <!--分页相关 View-->
    <div id="laypage2" class="layui-laypage-btn"></div>
</div>
<h2>已订阅列表</h2>
<!--一订阅课程列表-->
<table class="layui-hide" id="subscribe_course_table" lay-filter="subscribe_course_table"></table>
<!--分页相关 View-->
<div id="laypage" class="layui-laypage-btn"></div>
</body>
<script src="/layui/layui.js"></script>
<script src="/js/core.util.js"></script>
<script src="/js/xm-select.js"></script>
<script type="text/html" id="tool">
    <a class="layui-btn layui-btn-xs" lay-event="subscribe">订阅</a>
</script>
<script type="text/html" id="tool2">
    <a class="layui-btn layui-btn-xs" lay-event="unsubscribe">取消订阅</a>
</script>
<script>
    layui.use(['jquery', 'form', 'table', 'laypage', 'layer', 'laydate', 'tree', 'transfer', 'layedit'], function () {
        var $ = layui.jquery;
        var form = layui.form;
        var table = layui.table;
        var laypage = layui.laypage
        var layer = layui.layer;
        var laydate = layui.laydate;

        var searchParam = {
            current: 1,
            size: 10,
            /*以下为多条件查询参数*/
            name: null
        }

        var searchParam2 = {
            current: 1,
            size: 10,
            /*以下为多条件查询参数*/
            name: null
        }

        /**
         *  加载分页数据
         */
        function loadPageInfoSubscribed() {
            CoreUtil.sendAjax("/study/course/subscribed", searchParam, function (res) {
                //初始化分页器
                laypageTable(res.total, searchParam.current);
                //初始化渲染数据
                loadTable(res.records);
                if (res.records != null) {
                    loadTable(res.records);
                }
            }, "GET", false, function (res) {
                //用户列表接口无权限回调
                layer.msg("抱歉！您暂无获取用户列表的权限");
                var noAuthorityData;
                noAuthorityData = [];
                loadTable(noAuthorityData);
            });
        }

        //渲染分页插件
        var laypageTable = function (count, currentPage) {
            laypage.render({
                elem: 'laypage'
                , count: count
                , limit: searchParam.size
                , layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
                , curr: location.hash.replace('#!currentPage=', '')     //获取起始页
                , hash: 'currentPage'                                   //自定义hash值
                , jump: function (obj, first) {
                    if (!first) {
                        searchParam.current = obj.curr;
                        searchParam.size = obj.limit;
                        loadPageInfoSubscribed();
                    }
                }
            });
        };
        //渲染table
        var loadTable = function (data) {
            //展示已知数据
            table.render({
                elem: '#subscribe_course_table'
                // 这个 data 为数组数据，直接 放到 data 上即可。
                , data: data
                , even: true
                , limit: data.length
                , limits: [10, 20, 30, 40, 50]
                , cols: [
                    [
                        {type: 'checkbox', fixed: 'left'},
                        {field: 'id', title: 'ID', width: 300, sort: true},
                        {field: 'name', title: '课程名', width: 430},
                        {field: 'teacherName', title: '老师名', width: 250},
                        {field: 'introduce', title: '简介', width: 350},
                        {width: 300, toolbar: "#tool2", title: '操作'}
                    ]
                ]
            });
        };
        loadPageInfoSubscribed();

        /**
         *  加载分页数据
         */
        function loadPageInfoNoSubscribed() {
            CoreUtil.sendAjax("/study/course/noSubscribed", searchParam2, function (res) {
                //初始化分页器
                laypageTable2(res.total, searchParam2.current);
                //初始化渲染数据
                loadTable2(res.records);
            }, "GET", false, function (res) {
                //用户列表接口无权限回调
                layer.msg("抱歉！您暂无获取用户列表的权限");
                var noAuthorityData;
                noAuthorityData = [];
                loadTable2(noAuthorityData);
            });
        }

        //渲染分页插件
        var laypageTable2 = function (count, currentPage) {
            laypage.render({
                elem: 'laypage2'
                , count: count
                , limit: searchParam2.size
                , layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
                , curr: location.hash.replace('#!currentPage=', '')     //获取起始页
                , hash: 'currentPage'                                   //自定义hash值
                , jump: function (obj, first) {
                    if (!first) {
                        searchParam2.current = obj.curr;
                        searchParam2.size = obj.limit;
                        loadPageInfoNoSubscribed();
                    }
                }
            });
        };
        //渲染table
        var loadTable2 = function (data) {
            //展示已知数据
            table.render({
                elem: '#notSubscribed_course_table'
                // 这个 data 为数组数据，直接 放到 data 上即可。
                , data: data
                , even: true
                , limit: data.length
                , limits: [10, 20, 30, 40, 50]
                , cols: [
                    [
                        {type: 'checkbox', fixed: 'left'},
                        {field: 'id', title: 'ID', width: 300, sort: true},
                        {field: 'name', title: '课程名', width: 430},
                        {field: 'teacherName', title: '老师名', width: 250},
                        {field: 'introduce', title: '简介', width: 350},
                        {width: 300, toolbar: "#tool", title: '操作'}
                    ]
                ]
            });
        };
        loadPageInfoNoSubscribed();

        //执行查询
        $("#search").click(function () {
            searchParam.title = $("#name").val();
            searchParam.current = 1;
            loadPageInfoNoSubscribed();
        });

        // 订阅
        table.on('tool(notSubscribed_course_table)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'subscribe':
                    CoreUtil.sendAjax("/study/course/order/" + data.id, null, function (res) {
                        loadPageInfoNoSubscribed()
                        loadPageInfoSubscribed()
                    }, "POST", false, function (res) {
                        //用户列表接口无权限回调
                        layer.msg("抱歉！您暂无获取用户列表的权限");
                        var noAuthorityData;
                        noAuthorityData = [];
                        loadTable2(noAuthorityData);
                    });
                    break;
            }
        });
        var showMaterialList = function (courseId) {
            let showMaterialList = function (res) {
                let map = res.map(material => {
                    let url = material.src
                    return `<li><h4 style="display: inline-block">${material.title}</h4><a style="margin-left: 40px;color: red" href=${url}>下载</a></li>`
                });
                layer.open({
                    area: ['500px', '300px'],
                    title: "课程资料列表",
                    content: `
                            <ul style="list-style-type:circle">
                                   ${map}
                            </ul>
                                `,
                });
            }
            loadCourseMaterials(courseId, showMaterialList)
        }
        var loadCourseMaterials = function (courseId, success) {
            CoreUtil.sendAjax(`/study/courseMaterials/course/${courseId}`, null, success, "GET", false, function (res) {
                //更新课程无权限提示
                layer.msg("抱歉！您暂权限");
            });
        }

        // 取消订阅
        table.on('tool(subscribe_course_table)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'unsubscribe':
                    CoreUtil.sendAjax("/study/course/order/" + data.id, null, function (res) {
                        loadPageInfoNoSubscribed()
                        loadPageInfoSubscribed()
                    }, "DELETE", false, function (res) {
                        //用户列表接口无权限回调
                        layer.msg("抱歉！您暂无获取用户列表的权限");
                        var noAuthorityData;
                        noAuthorityData = [];
                        loadTable2(noAuthorityData);
                    });
                    break;
                case   'show':
                    showMaterialList(data.id);
                    break;
            }
        });
    });
</script>
</html>